package com.runen.infinitemovement.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.ColumnDetailsActivity;
import com.runen.infinitemovement.activity.LongCollegeActivity;
import com.runen.infinitemovement.adapter.ColumnAdapter;
import com.runen.infinitemovement.model.api.ActivityAPI;
import com.runen.infinitemovement.model.entity.CollageInfoEntity;
import com.runen.infinitemovement.model.entity.InformationListEntity;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.AlertUtils;
import com.runen.rxnetlibrary.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CollageInfoFragment extends Fragment {
    @BindView(R.id.rv_news)
    RecyclerView rvNews;
    @BindView(R.id.tv_check_more)
    TextView tvCheckMore;
    private View view;
    private ColumnAdapter columnAdapter;
    private int flag;

    public CollageInfoFragment(int flag) {
        this.flag = flag;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_collage_info, container, false);
            ButterKnife.bind(this, view);
            initView();
            initData();
        }
        return view;
    }

    private void initData() {
        AlertUtils.showProgressDialog(getActivity(), "", "加载中...", true);
        new ActivityAPI().getMyLongCollageInfo("", new BaseResultCallback<CollageInfoEntity>() {
            @Override
            public void onResponse(CollageInfoEntity response) {
                AlertUtils.closeProgressDialog();
                if (response.code == 0) {
                    if (response.data != null) {
                        List<InformationListEntity> listEntities = new ArrayList<>();
                        if (response.data.informationList != null && response.data.informationList.size() > 0) {
                            for (int i = 0; i < response.data.informationList.size(); i++) {
                                if (i < 3) {
                                    listEntities.add(response.data.informationList.get(i));
                                }
                            }
                            columnAdapter.setNewData(listEntities);
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }
            }
        });
    }

    private void initView() {
        /**
         * 长征新闻
         */
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getActivity());
        layoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        rvNews.setLayoutManager(layoutManager3);
        columnAdapter = new ColumnAdapter(getActivity());
        rvNews.setAdapter(columnAdapter);

        columnAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), ColumnDetailsActivity.class);
                intent.putExtra("informationId", columnAdapter.getData().get(position).informationId);
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.tv_check_more)
    public void onViewClicked() {
        Intent longCollege = new Intent(getActivity(), LongCollegeActivity.class);
        startActivity(longCollege);
    }
}
